package com.hmall.search.listener;

import com.hmall.common.constants.MqConstants;
import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ItemStatusListener {
    @Autowired
    private ISearchService searchService;


    //监听商品上架的消息，将商品保持到es
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item.up.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MqConstants.ITEM_UP_KEY
    ))
    public void handleItemUp(Long itemId) throws IOException {
        searchService.saveItemById(itemId);
    }

    //监听商品下架的消息，将商品从es中删除
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item.down.queue",durable = "true"),
            exchange = @Exchange(value = MqConstants.ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = MqConstants.ITEM_DOWN_KEY
    ))
    public void handleItemDown(Long itemId) {
        searchService.deleteItemById(itemId);
    }
}
